<?php
/* 
 * The site class, this class handles all url requests, and builds the current page according
*/
defined('SECUR_CHECK') or die("Security error");

class site {
    var $vars;
    var $template;
    var $currentpage;
    var $tpl_to_load;
    var $scripts;
    var $styles;

    function  __construct() {
        global $conf;
        global $user;
        $this->vars = $this->get_url_vars();
        $this->currentpage = $this->get_current_page();
        $this->scripts = array();
        $this->styles = array();
        if($user && $user->template != NULL) {
            $this->template = $user->template;
        }
        else {
            $this->template = $conf->default_template;
        }
    }
    /**
     *
     * @return <arrray> Array with request_uri variables
     */
    function get_url_vars() {
        $url = str_replace(RELPATH . "/", NULL, $_SERVER['REQUEST_URI']);
        $url = explode("?", $url);
        $url = explode("/", $url[0]);
        if(count($url) == 1 && $url[0] == '') $url = false;
        return $url;
    }
    /**
     *  Return the page that should be load from the include directory
     * @global <object> $conf
     * @return <string> string of the page to load (/includes/pages/*.php
     */
    function get_current_page() {
        global $conf;
        if($this->vars == false) {
            return $conf->default_page;
        }
        else {
            $vars = $this->vars;
            if($this->page_check($vars[0])) {
                // Page exists in /pages dir, check if the user is allowed on this page
                if($this->user_level_check($vars[0])) {
                    // User may view this page
                    return $vars[0];
                }
                else {
                    // no access allowed for this user
                    return $conf->default_page;
                }
            }
            elseif($vars[0] == "ajax" && isset($vars[1])) {
                // page requested ia an ajax one
                $ajaxpage = "ajax/".$vars[1];
                if($this->page_check($ajaxpage)) {
                    return $ajaxpage;
                }
                else {
                    return $conf->default_page;
                }
            }
            else {
                // page does not exist
                return $conf->default_page;
            }
        }
    }
    /**
     *  Checks if the user level is sufficient for the requested page
     * @global <object> $database
     * @global <object> $user
     * @param <string> $page
     * @return <BOOLEAN>  true is yes, false if no
     */
    function user_level_check($page) {
        global $database;
        global $user;
        $pagedata = $database->sql_array("SELECT menu_userlevel FROM brm_menu WHERE menu_alias = '$page'");
        if($pagedata[0]['menu_userlevel'] <= $user->level) {
            return true;
        }
        else {
            return false;
        }
    }
    /**
     *  Check if requested page exists
     * @param <type> $page
     * @return <type>
     */
    function page_check($page) {
        if(is_file(ABSPATH."/includes/pages/".$page.".php")) {
            return true;
        }
        else {
            return false;
        }
    }
    /**
     *  Set template files to load by smarty
     * @param <array> $tpl_array
     */
    function set_templates($tpl_array) {
        $this->tpl_to_load = $tpl_array;
    }
    function build_menu() {
        global $database;
        global $user;
        $menu_array = array();
        $i=0;
        $menu_data = $database->sql_array("SELECT * FROM brm_menu WHERE menu_userlevel <= " . $user->level . " AND menu_show = 1 AND menu_parent = 0 ORDER BY menu_order ASC");
        $menu_children_data = $database->sql_array("SELECT * FROM brm_menu WHERE menu_userlevel <= " . $user->level . " AND menu_show = 1 AND menu_parent > 0 ORDER BY menu_order ASC");
        if($menu_children_data) {
            foreach($menu_children_data as $row) {
                $menu_children_array[$row['menu_parent']][$i]['name'] = $row['menu_name'];
                $menu_children_array[$row['menu_parent']][$i]['link'] = RELPATH . "/" . $row['menu_alias'];
                $menu_children_array[$row['menu_parent']][$i]['active'] = ($this->currentpage == $row['menu_alias']) ? TRUE : FALSE;
                $i++;
            }
        }
        $i=0;
        $j=0;
        if($menu_data) {
            foreach($menu_data as $row) {
                $menu_array[$i]['name'] = $row['menu_name'];
                $menu_array[$i]['link'] = ($row['menu_link'] == '1') ? RELPATH . "/" . $row['menu_alias'] : FALSE;
                $menu_array[$i]['active'] = ($this->currentpage == $row['menu_alias']) ? TRUE : FALSE;
                if(isset($menu_children_array[$row['id']]))
                {
                    // Has children
                    foreach($menu_children_array[$row['id']] as $child)
                    {
                        $menu_array[$i]['children'][$j]['name'] = $child['name'];
                        $menu_array[$i]['children'][$j]['link'] = $child['link'];
                        $menu_array[$i]['children'][$j]['active'] = ($this->currentpage == $child['link']) ? TRUE : FALSE;
                        $j++;
                    }
                    $j=0;
                }
                else
                {
                    // Has no children
                    $menu_array[$i]['children'] = FALSE;
                }
                $i++;
            }
        }
        return $menu_array;
    }
    function reset_scripts()
    {
        $this->scripts = array();
    }
    function add_script($path_to_script) {
        $this->scripts[] = $path_to_script;
    }
    function add_style($path_to_style) {
        $this->styles[] = $path_to_style;
    }
    function check_email($email) {
        if (preg_match("/^(\w+((-\w+)|(\w.\w+))*)\@(\w+((\.|-)\w+)*\.\w+$)/",$email)) {
            return true;
        }
        else {
            return false;
        }

    }
}
?>
